package com.cloud.punish.workflow.command.create;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;

import com.cloud.punish.common.context.HumanSession;
import com.cloud.punish.common.exception.BizException;
import com.cloud.punish.common.utils.SessionHelper;
import com.cloud.punish.interfaces.FeignClientFreeflow;
import com.cloud.punish.workflow.bean.WfActDef;
import com.cloud.punish.workflow.command.Command;
import com.cloud.punish.workflow.command.WfRequest;
import com.cloud.punish.workflow.command.WfResponse;
import com.cloud.punish.workflow.entity.WfActInst;
import com.cloud.punish.workflow.entity.WfItemInst;
import com.cloud.punish.workflow.enums.WfActStateEnum;
import com.cloud.punish.workflow.enums.WfItemTypeEnum;
import com.cloud.punish.workflow.mapper.WfActInstMapper;
import com.cloud.punish.workflow.mapper.WfItemInstMapper;

/**
 * 流程创建指令
 * 
 * @author taominglong
 *
 */
public class CreateCommand extends Command {

	@Override
	public void execute(WfRequest request, WfResponse resp, ApplicationContext context) {
		CreateRequest req = (CreateRequest) request;
		long procDefId = req.getProcDefId();
		HumanSession humanSession = SessionHelper.get();
		List<Long> roleIds = SessionHelper.get().getRoleIds();
		if(null == roleIds){
			roleIds = new ArrayList<Long>();
		}
		roleIds.add(humanSession.getUnitId());
		roleIds.add(humanSession.getHumanId());
		// 检测当前用户是否在流程开始节点中
		FeignClientFreeflow feignClientFreeflow = context.getBean(FeignClientFreeflow.class);
		WfActDef start = feignClientFreeflow.loadStartActByProcAndPart(procDefId, StringUtils.join(roleIds, ","));
		if (null == start) {
			throw new BizException(500, "流程权限配置错误，不存在包含当前登录用户的流程参与者");
		}
		req.setStartAct(start);
		// 插入流程实例表
		WfActInstMapper wfActInstMapper = context.getBean(WfActInstMapper.class);
		WfActInst actInst = new WfActInst();
		BeanUtils.copyProperties(start, actInst);
		actInst.setRecId(req.getRecId());
		actInst.setPartId(humanSession.getHumanId());
		actInst.setPartName(humanSession.getHumanName());
		actInst.setPartType("H");
		actInst.setActStateId(WfActStateEnum.PENDING.getActStateId());
		actInst.setActStateName(WfActStateEnum.PENDING.getActStateName());
		actInst.setCreateTime(new Date());
		wfActInstMapper.insert(actInst);

		// 操作日志表
		WfItemInstMapper wfItemInstMapper = context.getBean(WfItemInstMapper.class);
		WfItemInst itemInst = new WfItemInst();
		itemInst.setActId(actInst.getActId());
		itemInst.setActDefId(actInst.getActDefId());
		itemInst.setActDefName(actInst.getActDefName());
		itemInst.setActionTime(new Date());
		itemInst.setActionName("启动流程");
		itemInst.setDetail(humanSession.getHumanName() + "在" + start.getActDefName() + "阶段启动流程");
		itemInst.setHumanId(humanSession.getHumanId());
		itemInst.setHumanName(humanSession.getHumanName());
		itemInst.setItemTypeId(WfItemTypeEnum.CREATE.getItemTypeId());
		itemInst.setItemTypeName(WfItemTypeEnum.CREATE.getItemTypeName());
		itemInst.setRecId(req.getRecId());
		wfItemInstMapper.insert(itemInst);

		// 设置信息以供后面的处理器使用
		CreateResponse cResp = (CreateResponse)resp;
		cResp.setActId(actInst.getActId());
		cResp.setActDef(start);
	}

}
